package com.pss.order.sharding;

import com.google.common.collect.Range;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Collection;

/**
 * standard分片策略的范围分片算法实现类
 * 实现接口 PreciseShardingAlgorithm<T>， 泛型类T是分片键的字段类型
 */
public class TableRangeAlgorithm implements RangeShardingAlgorithm<Long> {

    /**
     *  范围分片算法：用于处理BETWEEN AND, >, <, >=, <=分片
     * @param availableTargetNames - 数据源或者实际表的名字集合
     * @param shardingValue - 分片值
     * @return 返回分片后的实际表名
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) {

        //实现范围查询 cid between 200 and 300 中的上限和下限值
        Range<Long> valueRange = shardingValue.getValueRange();
        Long lower = valueRange.lowerEndpoint();   //下限值200
        Long upper = valueRange.upperEndpoint();   //上限值200
        //下面自行实现逻辑判断分片后的实际表名，这里就不具体实现了
        return availableTargetNames;
    }
}
